package com.skyolder.crutch.word.raw;

import java.util.ArrayList;

import com.skyolder.crutch.error.GrammarError;

public class BufferCharQueue {
	BufferCharPiper bufferCharPiper;
	private ArrayList<Character> charsBuffer=new ArrayList<Character>();
	private int position=0;
	public BufferCharQueue(BufferCharPiper bufferCharPiper){
		this.bufferCharPiper=bufferCharPiper;
	}
	public  char nextChar() throws GrammarError {
		//return bufferCharPiper.nextChar();
		if(charsBuffer.size()>0){
			position--;
			return charsBuffer.remove(0);
		}
		return bufferCharPiper.nextChar();
	}
	public char peekChar() throws GrammarError{
		//return bufferCharPiper.peekChar();
		if(position<charsBuffer.size()){
			position++;
			return charsBuffer.get(position-1);
		}
		char c=bufferCharPiper.peekChar();
		charsBuffer.add(c);
		position++;
		return charsBuffer.get(position-1);
	}
	public void back(){
		if(position>0){
			position--;
		}
	}
	public void back(int i){
		position-=i;
		if(position<0){
			position=0;
		}
	}
	public void reset(){
		position=0;
	}
	public String peekN(int n) throws GrammarError{
		StringBuilder stringBuilder=new StringBuilder();
		for(int i=0;i<n;++i){
			stringBuilder.append(bufferCharPiper.peekChar());
		}
		return stringBuilder.toString();
	}
	public void eatN(int n) throws GrammarError{
		for(int i=0;i<n;++i){
			bufferCharPiper.nextChar();
		}
	}
}
